<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Action\PageJumpAction;
use App\Http\Services\CourseOrderService;
use App\Http\Traits\GlobalConstant;
use App\Models\Campus;
use App\Models\Course;
use App\Models\CourseOrder;
use App\Models\Teacher;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CourseOrderController extends BaseController
{

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('报名列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('报名详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('更新报名状态')
            ->description('更新报名状态')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增报名')
            ->description('新增报名')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CourseOrder());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->like('order_sn', '订单号');
                $filter->equal('campusInfo.id', '校区')->select(Campus::where('status', 1)->pluck('name as text', 'id'));
                $filter->equal('courseInfo.id', '课程')->select(Course::where('valid', 1)->pluck('name as text', 'id'));

            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('userInfo.nickname', '用户');
                $filter->like('contact', '电话');
                $filter->equal('status', '状态')->select([-1 => '已取消', 1 => '排队中', 2 => '待上课', 3 => '已结课', 4 => '报名失败']);
            });

        });
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', '#')->sortable();

        $grid->column('order_sn', '订单号');
        $grid->column('userInfo.nickname', '用户')->sortable();
        $grid->column('campusInfo.name', '校区')->sortable();
        $grid->column('courseInfo.name', '课程')->sortable();
        $grid->column('courseInfo.start_time', '开课日期')->sortable();
        $grid->column('courseInfo.class_time', '上课时间')->sortable();
//        $grid->column('price', '价格');
        $grid->column('nickname', '姓名');
        $grid->column('sex', '性别')->sortable()->display(function ($v) {
            return GlobalConstant::SEX_LIST[$v];
        });
        $grid->column('age', '年龄')->sortable();
        $grid->column('contact', '电话');
        $grid->column('address', '地址');
        $grid->column('status', __('状态'))->sortable()->display(function ($v) {
            return CourseOrderService::showOrderStatusTip($this);
        })->label(['success', 'primary', 'danger', 'info', 'warning']);
//        $grid->column('cancel_at', '取消时间');
        $grid->column('created_at', '报名时间')->sortable();

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
            $actions->add(new PageJumpAction("更改状态", '/admin/course-order/' . $actions->row->id . '/edit'));
            $actions->add(new PageJumpAction("详情", '/admin/course-order/' . $actions->row->id));
        });

        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
//        $grid->expandFilter();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CourseOrder::findOrFail($id));
        $show->field('id', 'ID');
        $show->field('order_sn', '订单编号');
        $show->field('userInfo.nickname', '用户');
        $show->field('campusInfo.name', '校区');
        $show->field('courseInfo.name', '课程');
        $show->field('price', '价格');
        $show->field('status', '状态')->as(function ($v) {
            return CourseOrderService::showOrderStatusTip($this);
        });
        $show->field('cancel_at', '取消时间')->as(function ($v) {
            return $v == 0 ? '' : date('Y-m-d H:i:s', $v);
        });
        $show->field('cancel_reason', '取消原因');
        $show->field('nickname', '昵称');
        $show->field('sex', '性别');
        $show->field('age', '年龄');
        $show->field('contact', '联系方式');
        $show->field('address', '地址');
//        $show->courseSectionInfo('课程章节', function ($section) {
//            $section->name('章节名称');
//            $section->serial_number('章节序号');
//            $section->class_room('上课教室');
//            $section->class_time('上课时间');
//            $section->begin_time('开始时间');
//            $section->end_time('结束时间');
//            $section->paginate(50);
//            $section->disableActions();
//            $section->disableRowSelector();
//            $section->disableFilter();
//            $section->disableExport();
//            $section->disableCreateButton();
//            $section->disableColumnSelector();
//            $section->disablePerPageSelector();
//            $section->disablePagination();
//        });
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
                $tools->disableEdit();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new CourseOrder());
        $form->display('order_sn', '订单号');
        $form->display('userInfo.nickname', '用户');
        $form->display('campusInfo.name', '校区');
        $form->display('courseInfo.name', '课程');
        $form->display('nickname', '姓名');
        $form->display('sex', '性别');
        $form->display('age', '年龄');
        $form->display('contact', '电话');
        $form->display('address', '地址');
        $form->select('status', '状态')->options([-1 => '已取消', 1 => '排队中', 2 => '待上课', 3 => '已结课', 4 => '报名失败']);
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }
}
